"""
@File    : RunShell.py
@Time    : 2019/7/19 16:53
@Author  : LiuFeiYu
@Email   : liufeiyu@sunrise.net
@Software: PyCharm
"""
from Common import Shell
from time import sleep
from Settings import Config
import random
import pytest
import sys

MAX_LEN = 46


def run_pytest(args):
    cmd_result = Shell.Shell.invoke(f'pytest -s -q -m {args} --tb=line')
    print(cmd_result)
    print('3秒后返回主菜单')
    sleep(3)
    menu()


def menu():
    line_1 = '* [1]查询当前可执行用例函数'
    line_2 = '* [2]执行指定标记用例'
    line_3 = '* [3]执行所有用例'
    line_4 = '* [4]查询所有自定义标记用例'
    line_5 = '* [5]退出系统'

    print()
    print('*' * 16 + 'Pytest System v1.1' + '*' * 16)
    print(' ' * 2 + line_1)
    print(' ' * 2 + line_2)
    print(' ' * 2 + line_3)
    print(' ' * 2 + line_4)
    print(' ' * 2 + line_5)
    print('*' * 50)

    choice_num = input('请选择执行的功能代码：')
    choice_num = int(choice_num.strip())
    modular_control(choice_num)


def modular_control(modular_num):
    """用于控制整个模块的函数"""
    if modular_num == 1:
        show_all_test_case()
    elif modular_num == 2:
        execute_special_test_case()
    elif modular_num == 3:
        execute_all_test_case()
    elif modular_num == 4:
        show_all_mark()
        print('3秒后返回主菜单！')
        sleep(3)
        menu()
    elif modular_num == 5:
        exit_system()
    else:
        print('输入错误,重新输入')


def mark():
    print('-' + '-' * MAX_LEN + '-')


def show_all_mark():
    """展示所有mark标记"""
    a = Config.Config()
    res: str = a.get_pytest_cof('pytest', 'markers')
    mark_name = []
    mark_remarks = []
    for i in res.split('\n'):
        mark_name.append(len(i.split(':')[0].strip()))
        mark_remarks.append(len(i.split(':')[1].strip()))
    len_max = max(mark_name) + 10
    len_max_r = max(mark_remarks) + 10
    title = ['\t标记名', '备注']
    mark()
    print('{0:<{max2}}\t{1:<{max}}'.format(title[0],
                                           title[1],
                                           max=len_max,
                                           max2=len_max - 2))
    mark()
    for i in res.split('\n'):
        print('{0:<{max}}\t{1:<{max2}}'.format('\t' + i.split(':')[0].strip(),
                                               i.split(':')[1].strip(),
                                               chr(12288),
                                               max=len_max, max2=len_max_r))
        mark()


def show_all_test_case():
    """查询当前可执行用例函数"""
    cmd = 'pytest --collect-only'
    cmd_result = Shell.Shell.invoke(cmd)
    print(cmd_result)
    print('3秒后返回主菜单！')
    sleep(3)
    menu()


def execute_special_test_case():
    """执行指定用例函数"""
    show_all_mark()
    mark_name = input('请输入指定执行用例的标记名称：')
    run_pytest(mark_name)


def execute_all_test_case():
    """执行所有用例函数"""
    cmd_result = Shell.Shell.invoke('pytest --tb=line')
    print(cmd_result)
    print('3秒后返回主菜单！')
    sleep(3)
    menu()


def exit_system():
    """退出系统"""
    res = input('确定当前操作吗？[y/n]：')
    if res in ['y', 'Y']:
        print('欢迎下次使用！')
        sys.exit()
    elif res in ['n', 'N']:
        menu()
    else:
        print('请输入y或者n')
        exit_system()


if __name__ == '__main__':
    menu()
